package scau.hotel.hotel_service.util;

import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 校验工具类
 */
public class ValidatorUtil {

    //房间编号校验正则表达式
    private static final Pattern roomNo_pattern = Pattern.compile("[1-3][0-9]{2}$");
    //房型编号校验正则表达式
    private static final Pattern roomTypeNo_pattern = Pattern.compile("[2][4][0-9]{3}$");
    //正整数校验正则表达式
    private static final Pattern positive_integer_pattern = Pattern.compile("^[1-9]\\d*$");
    //汉字校验正则表达式
    private static final Pattern chinese_pattern = Pattern.compile("[\\u4E00-\\u9FA5\\\\s]+");
    //金额数校验正则表达式
    private static final Pattern money_pattern = Pattern.compile("^(([1-9]{1}\\d*)|(0{1}))(\\.\\d{1,2})?$");
    //姓名校验正则表达式
    private static final Pattern name_pattern = Pattern.compile("^([\\u4e00-\\u9fa5]{1,20}|[a-zA-Z\\.\\s]{1,20})$");
    //手机号码校验正则表达式
    private static final Pattern mobilePhone_pattern = Pattern.compile("^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$");
    //身份证校验正则表达式
    private static final Pattern idCard_pattern = Pattern.compile("(([1-9][0-9]{5}[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{3})|([1-9][0-9]{5}(18|19|20)[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{3}[0-9Xx]))");

    //校验roomNo
    public static boolean isRoomNo(String roomNo){
        if(StringUtils.isEmpty(roomNo)){
            return false;
        }
        Matcher matcher = roomNo_pattern.matcher((roomNo));
        return matcher.matches();
    }
    //校验roomTypeNo
    public static boolean isRoomTypeNo(String roomTypeNo){
        if(StringUtils.isEmpty(roomTypeNo)){
            return false;
        }
        Matcher matcher = roomTypeNo_pattern.matcher((roomTypeNo));
        return matcher.matches();
    }
    //校验正整数
    public static boolean isPositiveInteger(String s){
        if(StringUtils.isEmpty(s)){
            return false;
        }
        Matcher matcher = positive_integer_pattern.matcher((s));
        return matcher.matches();
    }
    //校验汉字
    public static boolean isChinese(String s){
        if(StringUtils.isEmpty(s)){
            return false;
        }
        Matcher matcher = chinese_pattern.matcher((s));
        return matcher.matches();
    }
    //校验最多两位小数的金额
    public static boolean isMoney(String s){
        if(StringUtils.isEmpty(s)){
            return false;
        }
        Matcher matcher = money_pattern.matcher((s));
        return matcher.matches();
    }
    //校验姓名
    public static boolean isName(String s){
        if(StringUtils.isEmpty(s)){
            return false;
        }
        Matcher matcher = name_pattern.matcher((s));
        return matcher.matches();
    }
    //校验姓名
    public static boolean isIDCard(String s){
        if(StringUtils.isEmpty(s)){
            return false;
        }
        Matcher matcher = idCard_pattern.matcher((s));
        return matcher.matches();
    }
    //校验手机号
    public static boolean isMobilePhone(String s){
        if(StringUtils.isEmpty(s)){
            return false;
        }
        Matcher matcher = mobilePhone_pattern.matcher((s));
        return matcher.matches();
    }
}
